package com.yc.sole.system.biz;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.yc.sole.framework.exception.BaseException;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.SequenceRuleDto;
import com.yc.sole.system.enums.SequencePrefixRule;
import com.yc.sole.system.repository.SequenceRuleRepository;
import com.yc.sole.system.request.SequenceRuleQueryReq;
import com.yc.sole.system.request.SequenceRuleReq;
import com.yc.sole.system.response.SequenceRuleResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-03-06
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class SequenceRuleBiz {

    private final SequenceRuleRepository sequenceRuleRepository;

    /**
     * 新增
     *
     * @param sequenceRuleReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveSequenceRule(SequenceRuleReq sequenceRuleReq) {
        SequenceRuleDto dto = new SequenceRuleDto();
        BeanUtil.copyProperties(sequenceRuleReq, dto);
        this.validateAndSet(dto);
        sequenceRuleRepository.saveSequenceRule(dto);
    }

    /**
     * 修改
     *
     * @param sequenceRuleReq
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSequenceRuleById(SequenceRuleReq sequenceRuleReq) {
        SequenceRuleDto dto = new SequenceRuleDto();
        BeanUtil.copyProperties(sequenceRuleReq, dto);
        this.validateAndSet(dto);
        sequenceRuleRepository.updateSequenceRuleById(dto);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSequenceRuleByIds(Collection<Long> ids) {
        sequenceRuleRepository.deleteSequenceRuleByIds(ids);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public SequenceRuleResp selectSequenceRuleById(Long id) {
        return BeanUtil.copyProperties(sequenceRuleRepository.selectSequenceRuleById(id), SequenceRuleResp.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param sequenceRuleQueryReq
     * @return
     */
    public Paging<SequenceRuleResp> selectSequenceRulePage(Paging<SequenceRuleQueryReq> page, SequenceRuleQueryReq sequenceRuleQueryReq) {
        Paging<SequenceRuleDto> paging = sequenceRuleRepository.selectSequenceRulePage(page, sequenceRuleQueryReq);
        return PageUtil.pageCopy(paging, SequenceRuleResp.class);
    }
    /**
     * 验证
     *
     * @param dto
     */
    private void validateAndSet(SequenceRuleDto dto) {
        if (!SequencePrefixRule.FIXED.equals(dto.getPrefixRule())) {
            dto.setPrefix(null);
        } else {
            if (CharSequenceUtil.isBlank(dto.getPrefix())) {
                throw new BaseException("请输入前缀");
            }
            if (CharSequenceUtil.length(dto.getPrefix()) >= dto.getLength()) {
                throw new BaseException("前缀长度不能大于或等于序列总长度");
            }
        }
    }

}
